package com.itcast.manager3.B_controller;

import com.itcast.manager3.C_service.ShareholderService;
import com.itcast.manager3.tomian.ShareholderTomain;

import java.util.ArrayList;
import java.util.Scanner;

public class ShareholderContorller {
    Scanner sc = new Scanner(System.in);
    ShareholderService shs = new ShareholderService();

    public void start() {
        while (true) {
            System.out.println("---欢迎光临黑马《股东》管理系统---");
            System.out.println("请输入您要选择的操作");
            System.out.println("1:增加股东");
            System.out.println("2:删除股东");
            System.out.println("3:修改股东");
            System.out.println("4:查看股东");
            System.out.println("5:退出");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    addShareholder();
                    break;
                case 2:
                    deleteShareholder();
                    break;
                case 3:
                    updateShareholder();
                    break;
                case 4:
                    findSheareholder();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }

    }

    private void updateShareholder() {
        System.out.println("请输入要修改的股东工号");
        String id = sc.next();
        if (getShareholderpetition(id) == false) {
            System.out.println("没有找到您所需要修改的股东工号，请重新输入");
            return;
        }
        System.out.println("请输入股东的名字");
        String name = sc.next();
        System.out.println("请输入股东的年龄");
        int age = sc.nextInt();
        System.out.println("请输入股东的生日");
        String birthday = sc.next();
        System.out.println("请输入股东的股份占比");
        String share = sc.next();
        ShareholderTomain sto = new ShareholderTomain(id,name,age,birthday,share)
        shs.updateShareholder(sto);

    }

    private void deleteShareholder() {
        System.out.println("请输入要删除的股东工号");
        String id = sc.next();
        if (getShareholderpetition(id) == false) {
            System.out.println("没有找到您所需要删除的股东工号，请重新输入");
            return;
        }
        shs.deleteShareholder(id);
        System.out.println("删除成功");

    }

    private void findSheareholder() {
        ArrayList<ShareholderTomain> shareholder = shs.findShareholder();
        if (shareholder.size() == 0) {
            System.out.println("还没有股东信息，请您输入后再进行查看");
            return;
        }
        for (int i = 0; i < shareholder.size(); i++) {
            ShareholderTomain shareholderTomain = shareholder.get(i);
            System.out.println(shareholderTomain.getId() + "--" + shareholderTomain.getName() + "--" + shareholderTomain.getAge() + "--" + shareholderTomain.getBirthday() + "--" + shareholderTomain.getShare());
        }
    }

    public void addShareholder() {
        System.out.println("请输入股东的工号");
        String id = sc.next();
        if (getShareholderpetition(id) == true) {
            System.out.println("已有股东工号，请您重新输入");
            return;
        }
        System.out.println("请输入股东的名字");
        String name = sc.next();
        System.out.println("请输入股东的年龄");
        int age = sc.nextInt();
        System.out.println("请输入股东的生日");
        String birthday = sc.next();
        System.out.println("请输入股东的股份占比");
        String share = sc.next();
        ShareholderTomain shd = new ShareholderTomain(id, name, age, birthday, share);
        shs.addShareholder(shd);
        System.out.println("添加成功");

    }

    public boolean getShareholderpetition(String id) {
        if (shs.isExists(id) == true) {
            return true;
        }
        return false;
    }
}
